package com.idol.logger.core.printer

import android.util.Log
import com.idol.logger.core.IdolManager
import com.idol.logger.core.printer.iface.IdolPrinter

object IdolConsolePrinter : IdolPrinter {

    private const val MAX_LEN = 512

    override fun print(level: Int, tag: String, printString: String) {
        if (IdolManager.debug) {
            printString.split("\n").forEach {
                val len = it.length
                when (val countOfSub = len / MAX_LEN) {
                    0 -> Log.println(level, tag, it)
                    else -> {
                        var index = 0
                        (0 until countOfSub).forEach { _ ->
                            Log.println(level, tag, it.substring(index, index + MAX_LEN))
                            index += MAX_LEN
                        }
                        if (index != len) Log.println(level, tag, it.substring(index, len))
                    }
                }
            }
        }
    }
}